home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: dannyyoo@ix.netcom.com (Danny Yoo)
- Newsgroups: comp.lang.c++
- Subject: Re: Help!
- Date: Mon, 01 Jan 1996 04:51:58 GMT
- Organization: Netcom
- Message-ID: <30e76088.1216430@nntp.ix.netcom.com>
- References: <4c2me5$ke6@news-e2a.gnn.com>
- NNTP-Posting-Host: ix-scr-ca1-13.ix.netcom.com
- X-NETCOM-Date: Sun Dec 31 9:00:02 PM PST 1995
- X-Newsreader: Forte Agent .99c/16.141
-
- Gary Hagen <GaryJ@gnn.com> wrote:
- >I am currently learning the C++ language.I need help with a
- >programming exercise.Here is the code.
-
-
- >Whatever number I enter for the carnum array of
- >structures,all it gives me is the last car and year that I
- >entered as many times as the entered carnum.I guess my
- >problem is that I'm not accessing each structure in the
- The problem I saw was that you're not doing anything with the
- increments. You have a pointer, ps, but it doesn't access the rest of
- your allocated space. You can access them by either incrementing the
- pointer itself:
- ps++;
- or you could just do what I did and just use pointer arithmetic.
-
- I modified the program (hopefully it should work). I've made
- a few modifications, some of them probably unwanted <grin>, but you
- can fix the rest of my convolted code. Here is the completed program:
-
- #include <iostream.h>
- struct car {
- char make[31];
- // since you limited the size of the name string, I set this
- // to 31
- int year;
- };
-
- void main(void) {
- const int MAX=100; // MAX is the maximum cars you want
-
- int temp, carnum;
- do {
- cout << "Please enter the number of cars to catalog:";
- cin >> carnum;
- }
- while(carnum < 1 || carnum > MAX); // I changed this part
- // from the ambiguous 'continue' that you used. I'm not
- // exactly sure if this is what you meant or not, but this
- //should limit the range from 1 to 100.
-
- car * ps = new car[carnum]; // allocates enough space
-
- for(temp=0; temp < carnum; temp++) {
- cout << "\nEnter make of car: ";
- cin >> (ps+temp)->make;
- cout << "Enter model year: ";
- cin >> (ps+temp)->year;
- cout << "\n\n";
- }
- // I use tem as my increment variable. (ps+0) is the first element,
- // (ps+1) the first, (ps+3) the third, etc...
- for(temp = 0;temp<carnum;temp++)
- cout << (ps+temp)->year << " " << (ps+temp)->make
- << "\n";
- }
-